package com.cskaoyan.market.util;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.cskaoyan.market.db.admin.vo.BaseStorageCreateVo;

/**
 * @author 小焦
 * @since 2023/03/19 00:29
 */
public class FileUploadUtils {
    private static String DEFAULT_IMGPATH = "img";
    private static String DEFAULT_URL = "http://localhost:8083/";

    private FileUploadUtils() {
    }

    public static BaseStorageCreateVo createVo;


    public static int uploadImg(HttpServletRequest req, HttpServletResponse resp) {
        // FileUploadUtils uploadFile = new FileUploadUtils();
        createVo = new BaseStorageCreateVo();
        try {
            Collection<Part> parts = req.getParts();
            for (Part part : parts) {
                if (part != null) {
                    // part.getContentType() = image/jpeg | image/png
                    String contentType = part.getContentType();
                    createVo.setType(contentType);

                    if (!"image/jpeg".equals(contentType) && !"image/png".equals(contentType)) {
                        // 文件类型不匹配
                        return 250;
                    }
                    // part.getSize() = 135454 ==136kb
                    long size = part.getSize();
                    if (size < 0 || size > 500000) {
                        // 文件大小不得超过500kb
                        return 251;
                    }
                    createVo.setSize((int) size);
                    // 文件类型 和文件大小都合适
                    LocalDateTime now = LocalDateTime.now();
                    String date = now.toString().substring(0, 10) + "/";
                    createVo.setAddTime(now);
                    createVo.setUpdateTime(now);
                    // 获取文件名
                    String filename = part.getSubmittedFileName();
                    if (filename != null) {
                        createVo.setName(filename);
                        // 文件----文件的上传操作
                        String uuid = UUID.randomUUID().toString();
                        // 1.jpeg-sadjisadjlskdad
                        // 作为 key
                        String filenameSalt = uuid + "-" + filename;
                        createVo.setKey(filenameSalt);

                        int hashCode = filenameSalt.hashCode();
                        String basePath = DEFAULT_IMGPATH +"/"+ date;
                        String hexString = Integer.toHexString(hashCode);
                        char[] chars = hexString.toCharArray();
                        for (char aChar : chars) {
                            // image/1/2/3/4/5/6/A/F
                            basePath = basePath + "/" + aChar;
                        }
                        String relativePath = basePath + "/" + filenameSalt;
                        createVo.setUrl(DEFAULT_URL+relativePath);
                        String path = req.getServletContext().getRealPath(relativePath);

                        File file = new File(path);
                        if (!file.getParentFile().exists()) {
                            // 创建父级路径
                            file.mkdirs();
                        }
                        part.write(path);
                    }
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
            return 252;
        } catch (ServletException e) {
            e.printStackTrace();
            return 252;
        }
        return 0;
    }


}
